﻿#region Copyright
//Copyright 2012 Phelon Aka. Jon H.
/*
    This file is part of BGBuddy - Ultimate PVP Suite.

    BGBuddy - Ultimate PVP Suite is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    BGBuddy - Ultimate PVP Suite is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Foobar.  If not, see <http://www.gnu.org/licenses/>
 */
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Threading;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using Styx.Plugins;
using Styx;
using Styx.Helpers;
using Styx.CommonBot.POI;
using Styx.WoWInternals.WoWObjects;
using Styx.WoWInternals;
using Styx.Pathing;
using Styx.Common.Helpers;

namespace UltimatePvPSuite
{
    public static class TargetHelper
    {
        private static WoWPlayer Me = StyxWoW.Me;
        public static WoWUnit Target;
        private static Dictionary<string, int> TargetList = new Dictionary<string, int>();
        private static List<string> MasterList;

        private readonly static WaitTimer TargetTimer = new WaitTimer(TimeSpan.FromMilliseconds(UltimatePvPSuite.TargetCheckFrequency * 1000));
        public static bool Pulse()
        {
            if (Targeting())
                return true;
            return false;
        }
        private static bool Targeting()
        {
            //Checks
            if (!UltimatePvPSuite.EnabledPriorityTargeting) { return false; }
            //Reset Timer
            if (TargetTimer.IsFinished)
            {
                TargetTimer.Reset();
            }
            else
            {
                return false;
            }
            //Clear Target
            Target = Me.CurrentTarget;
            if (Me.CurrentTarget != null)
            {
                if (Target.Mounted) return false;
                if (!Target.IsHostile) return false;
                if (!Target.Attackable) return false;
            }
            if (Me.CurrentTarget == null || !Me.CurrentTarget.IsAlive || UltimatePvPSuite.CurrentTarget == Me || UltimatePvPSuite.CurrentTarget == null)
            {
                UltimatePvPSuite.CurrentTarget = Me;
                UltimatePvPSuite.CurrentTargetType = "";
            }
            //Add list as Needed
            if (TargetList.Count < 1)
                TargetDictionary();
            //Sort the List
            List<KeyValuePair<string, int>> sorted = (from target in TargetList orderby target.Value descending select target).ToList();
            //Targetting
            foreach (KeyValuePair<string, int> target in sorted)
            {
                if (target.Key == "Demolishers" && Demolisher.TargetDemolishers())
                {
                    UltimatePvPSuite.CurrentTargetType = "Demolishers";
                    return true;
                }
                if (target.Key == "FlagCarriers" && FlagCarrier.TargetFlagCarrier())
                {
                    UltimatePvPSuite.CurrentTargetType = "FlagCarriers";
                    return true;
                }
                if (target.Key == "Healer" && Healer.TargetHealers())
                {
                    UltimatePvPSuite.CurrentTargetType = "Healer";
                    return true;
                }
                if (target.Key == "LowHealth" && LowHealth.TargetLowHealth())
                {
                    UltimatePvPSuite.CurrentTargetType = "LowHealth";
                    return true;
                }
                if (target.Key == "Undergeared" && Undergeared.TargetUndergeared())
                {
                    UltimatePvPSuite.CurrentTargetType = "Undergeared";
                    return true;
                }
                if (target.Key == "Totems" && Totems.TargetTotems())
                {
                    UltimatePvPSuite.CurrentTargetType = "Totems";
                    return true;
                }
            }
            return false;
        }

        private static void TargetDictionary()
        {
            TargetList.Add("Demolishers", Convert.ToInt32(UltimatePvPSuite.TargetDemolishersPriority));
            TargetList.Add("FlagCarriers", Convert.ToInt32(UltimatePvPSuite.TargetFlagCarriersPriority));
            TargetList.Add("Healer", Convert.ToInt32(UltimatePvPSuite.TargetHealersPriority));
            TargetList.Add("LowHealth", Convert.ToInt32(UltimatePvPSuite.TargetLowHealthPriority));
            TargetList.Add("Undergeared", Convert.ToInt32(UltimatePvPSuite.TargetUndergearedPriority));
            TargetList.Add("Totems", Convert.ToInt32(UltimatePvPSuite.TargetTotemsPriority));

        }

        public static bool TargetExists()
        {
            WoWUnit playerNear = ObjectManager.GetObjectsOfType<WoWUnit>().Where(
                                u => u.IsHostile && u.DistanceSqr <= 55 * 55 && u.IsPlayer && u.InLineOfSight).OrderBy(u => u.Distance).
                                FirstOrDefault();
            if (playerNear != null && !StyxWoW.Me.IsActuallyInCombat && UltimatePvPSuite.EnabledForceCombat)
            {
                UltimatePvPSuite.CurrentTarget = playerNear;
                return true;
            }
            return false;
        }

        public static void GetInCombat()
        {
            Logger.Write("Trying to Force Combat. Switching to " + ((WoWUnit)Target).SafeName() + "!");
            Navigator.MoveTo(UltimatePvPSuite.CurrentTarget.Location);
            BotPoi.Current = new BotPoi(UltimatePvPSuite.CurrentTarget, PoiType.Kill);
            UltimatePvPSuite.CurrentTarget.Target();
        }
    }
}
